Skip to content

Method: C4Solution(IC4Board, IC4SolutionSlim)

1: package de.fhdw.gaming.ipspiel23.c4.domain.impl;
2:
3: import java.util.Optional;
4:
5: import de.fhdw.gaming.ipspiel23.c4.domain.C4Direction;
6: import de.fhdw.gaming.ipspiel23.c4.domain.IC4Board;
7: import de.fhdw.gaming.ipspiel23.c4.domain.IC4Position;
8: import de.fhdw.gaming.ipspiel23.c4.domain.IC4Field;
9: import de.fhdw.gaming.ipspiel23.c4.domain.IC4Player;
10: import de.fhdw.gaming.ipspiel23.c4.domain.IC4Solution;
11: import de.fhdw.gaming.ipspiel23.c4.domain.IC4SolutionSlim;
12:
13: /**
14: * The default implementation of {@link IC4Solution}.
15: */
16: public class C4Solution implements IC4Solution {
17:
18: /**
19: * The parent board that contains this solution.
20: */
21: private final IC4Board board;
22:
23: /**
24: * The internal slim solution instance that is wrapped by this class.
25: */
26: private final IC4SolutionSlim slimSolution;
27:
28: /**
29: * Creates a new instance.
30: *
31: * @param board The parent board that contains this solution.
32: * @param slimSolution The internal slim solution instance that is wrapped by this class.
33: */
34: public C4Solution(final IC4Board board, final IC4SolutionSlim slimSolution) {
35: this.board = board;
36: this.slimSolution = slimSolution;
37: }
38:
39: @Override
40: public IC4Player getOwner() {
41: return this.slimSolution.getOwner();
42: }
43:
44: @Override
45: public IC4Position getStartPosition() {
46: return this.slimSolution.getStartPosition();
47: }
48:
49: @Override
50: public IC4Position getEndPosition() {
51: return this.slimSolution.getEndPosition();
52: }
53:
54: @Override
55: public C4Direction getDirection() {
56: return this.slimSolution.getDirection();
57: }
58:
59: @Override
60: public int size() {
61: return this.slimSolution.size();
62: }
63:
64: @Override
65: public IC4Field[] getContainingFields() {
66: final IC4Field[] fields = new IC4Field[size()];
67: final C4Direction dir = getDirection();
68: // size() will always be > 0 :)
69: Optional<IC4Field> field = this.board.tryGetField(getStartPosition());
70: // add fields until we reach the end of the solution, there are no neighboring fields, or
71: // we reach an empty field
72: for (int i = 0; i < fields.length && field.isPresent(); field = field.get().tryGetNeighbor(dir), i++) {
73: fields[i] = field.get();
74: }
75: return fields;
76: }
77:
78: @Override
79: public boolean equals(final Object object) {
80: if (object == this) {
81: return true;
82: }
83: if (!(object instanceof IC4Solution)) {
84: return false;
85: }
86: final IC4Solution other = (IC4Solution) object;
87: return this.getOwner().getToken() == other.getOwner().getToken()
88: // we don't care what direction a solution is facing.
89: // the direction itself should probably be internal, but I guess
90: // it may have valid use cases for consumers of our library :)
91: && (this.getStartPosition().equals(other.getStartPosition())
92: && this.getEndPosition().equals(other.getEndPosition())
93: || this.getStartPosition().equals(other.getEndPosition())
94: && this.getEndPosition().equals(other.getStartPosition()))
95: && this.size() == other.size();
96: }
97:
98: @Override
99: public int hashCode() {
100: int hash = 17 * 31;
101: hash += this.board.hashCode();
102: hash = hash * 31 + this.slimSolution.hashCode();
103: return hash;
104: }
105:
106: @Override
107: public String toString() {
108: return String.format("C4Solution[owner: %s, start: %s, end: %s, direction: %s]",
109: getOwner(), getStartPosition(), getEndPosition(), getDirection());
110: }
111: }